package com.hzit.seckill.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Component
public class LoginInteceptor implements HandlerInterceptor {
	public static ThreadLocal<String> loginUserName = new ThreadLocal<>();
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		//1. 得到session
		HttpSession session = request.getSession();
		//2. 得到session中的登录用户名
		Object name = session.getAttribute("name");
		//3. 如果有值就放到threadLocal中，没有值就登录
		if(name != null){
			loginUserName.set(name.toString());
			return true;
		}
		response.sendRedirect("http://auth.hzit.com/gitee/login?returnUrl=http://seckill.hzit.com");
		return false;
	}
}
